import 'dart:convert';
import 'dart:ui';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:mziot/app/config/constant.dart';
import 'package:mziot/app/data/user_entity.dart';
import 'package:mziot/app/tools/device.dart';
import 'package:mziot/generated/json/base/json_convert_content.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GlobalConst {
  static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  static PackageInfo? packageInfo;
  static final Connectivity connectivity = Connectivity();
  static SharedPreferences? sharedPreferences;
  static final netInfo = NetworkInfo();

  static Future<bool> asyns() async {
    sharedPreferences = await SharedPreferences.getInstance();
    packageInfo = await PackageInfo.fromPlatform();

    DeviceUtils.init();
    return Future.value(true);
  }

  static double screenWidth = window.physicalSize.width / window.devicePixelRatio;
  static double screenHeight = window.physicalSize.height / window.devicePixelRatio;
  static UserEntity? tempModel;
  static UserEntity? get userModel {
    if (tempModel == null) {
      final jsonStr = sharedPreferences?.getString(Constant.kUser);
      if (jsonStr == null) {
        return null;
      }
      final map = json.decode(jsonStr);
      final model = JsonConvert.fromJsonAsT<UserEntity>(map);
      tempModel = model;
    }

    return tempModel;
  }
}
